/*
* $Id: DeleteOperation.cpp 46 2010-02-28 22:01:59Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "DeleteOperation.h"
#include "../Tables/TableNames.h"
#include "../Tables/EvacuationTable.h"
#include "../Tables/FamiliesTable.h"
#include "../Tables/HousingsTable.h"
#include "../Tables/FieldValuesTable.h"
#include "../Tables/JournalTable.h"
#include "../Tables/PointsTable.h"
#include "../Tables/InspectTable.h"
#include "../SqlBinder.h"
#include "Event/EventId.h"

CDeleteOperation::CDeleteOperation(){}
CDeleteOperation::~CDeleteOperation(){}
void CDeleteOperation::DeleteValue(IDatabase* pDatabase){
	pDatabase->BeginTransaction(_T("Delete Operation"));
	if(m_content.s_type == content::HOUSING)
		DeleteHousing(pDatabase);
	else if(m_content.s_type == content::FAMILY){
		DeleteFamily(pDatabase);
		IApplication::GetApp()->GetEventManager()->SendEvent(FAMILIES_DB_CHANGE_EVENT);
	}else if(m_content.s_type == content::POINT){
		DeletePoint(pDatabase);
		IApplication::GetApp()->GetEventManager()->SendEvent(POINTS_DB_CHANGE_EVENT);
	}
	else if(m_content.s_type == content::JOURNAL)
		DeleteJournal(pDatabase);
	pDatabase->EndTransaction(_T("Delete Operation"));
	m_content.Clear();
}

CDeleteOperation::content::content(){
	Clear();
}
void CDeleteOperation::content::Clear(){
	s_type = DEFAULT;
	s_nId = 0;
}

void CDeleteOperation::DeleteHousing(IDatabase* pDatabase){
	CHousingsTable table;
	table.m_content.s_nId = m_content.s_nId;
	table.DeleteValue(pDatabase);
}
void CDeleteOperation::DeletePoint(IDatabase* pDatabase){
//delete all families of point;
	CFamiliesTable families;
	families.m_content.s_nIdPoint = m_content.s_nId;
	IResult* pResult = families.GetValue(pDatabase);
	for(int i = 0; i < (int)pResult->GetSizeResult(); i++)
		DeleteFamily(pDatabase, ((CFamiliesTable::content*)(*pResult)[i])->s_nId);
	pResult->Free();
//get id_record of field_values table
	CPointsTable points;
	points.m_content.s_nId = m_content.s_nId;
	pResult = points.GetValue(pDatabase);
//delete record of field_values table
	if(pResult->GetSizeResult()){
		CFieldValuesTable fieldValues;
		fieldValues.m_content.s_nId = ((CPointsTable::content*)(*pResult)[0])->s_nIdRecord;
		fieldValues.DeleteValue(pDatabase);
	}
	pResult->Free();
//delete point
	points.m_content.s_nId = m_content.s_nId;
	points.DeleteValue(pDatabase);
}

void CDeleteOperation::DeleteFamily(IDatabase* pDatabase, int nId){
	if(!nId)
		nId = m_content.s_nId;
//reset housing
	std::tstring strRequest(_T("UPDATE "));
	CSqlBinder binder(strRequest + HOUSINGS_TABLE_NAME
						+ _T(" SET id_family=0 WHERE id_family=?;"));
	binder.SetValue(0, nId);
	pDatabase->ExecRequest(binder.GetRequest());
//delete evacuation of family
	CEvacuationTable evacuation;
	evacuation.m_content.s_nIdFamily = nId;
	evacuation.DeleteValue(pDatabase);
//delete all inspects of family
	CJournalTable journal;
	journal.m_content.s_nIdFamily = nId;
	IResult* pResult = journal.GetValue(pDatabase);
	for(int i = 0; i < (int)pResult->GetSizeResult(); i++)
		DeleteJournal(pDatabase, ((CJournalTable::content*)(*pResult)[i])->s_nId);
	pResult->Free();
//get id_record of field_values table
	CFamiliesTable family;
	family.m_content.s_nId = nId;
	pResult = family.GetValue(pDatabase);
//delete record of field_values table
	CFieldValuesTable fieldValue;
	fieldValue.m_content.s_nId = ((CFamiliesTable::content*)(*pResult)[0])->s_nIdRecord;
	fieldValue.DeleteValue(pDatabase);
	pResult->Free();
//delete family
	family.m_content.s_nId = nId;
	family.DeleteValue(pDatabase);
}
void CDeleteOperation::DeleteJournal(IDatabase* pDatabase, int nId){
	if(!nId)
		nId = m_content.s_nId;
//delete all inspect of housing of current inspect;
	CInspectTable inspect;
	inspect.m_content.s_nIdJournal = nId;
	inspect.DeleteValue(pDatabase);
//get id_record of field_values table
	CJournalTable journal;
	journal.m_content.s_nId = nId;
	IResult* pResult = journal.GetValue(pDatabase);
//delete record of field_values table
	CFieldValuesTable fieldValue;
	fieldValue.m_content.s_nId = ((CJournalTable::content*)(*pResult)[0])->s_nIdRecord;
	fieldValue.DeleteValue(pDatabase);
	pResult->Free();
//delete current inspect
	journal.m_content.s_nId = nId;
	journal.DeleteValue(pDatabase);
}
